package com.hnevc.xiaoming.httpactivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.httpbean.RePassWordBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MyRegistered extends AppCompatActivity implements View.OnClickListener {

    private ImageView back;
    private TextView title_view;
    private EditText et_name_zc;
    private EditText et_password_zc;
    private EditText et_password_aszc;
    private Button btn_ok_zc;
    private String name;
    private String zc;
    private String aszc;
    private RePassWordBean rePassWordBean;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_registered);

        handler = new Handler();
        initView();

    }


    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        title_view = (TextView) findViewById(R.id.title_view);
        title_view.setText("注冊界面");

        et_name_zc = (EditText) findViewById(R.id.et_name_zc);
        et_password_zc = (EditText) findViewById(R.id.et_password_zc);
        et_password_aszc = (EditText) findViewById(R.id.et_password_aszc);
        btn_ok_zc = (Button) findViewById(R.id.btn_ok_zc);

        btn_ok_zc.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok_zc:
                if (submit()){
                    getRT();
                }
                break;
        }
    }

    private void getRT() {

        new Thread(new Runnable() {
            @Override
            public void run() {
//                try {
//                    rePassWordBean = OkHttpUtil.getOkHttpUtil().sendPostReq("http://anli-wechat.boruiman.com/api/UserServer/user_register",
//                            "username=" + name + "&password=" + zc + "&repassword=" + aszc + "", RePassWordBean.class);
//                    Log.e("TAG", "run: "+rePassWordBean.getMsg() );
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(MyRegistered.this, ""+rePassWordBean.getMsg(), Toast.LENGTH_SHORT).show();
//                        }
//                    });
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }

                OkHttpClient client = new OkHttpClient();

                RequestBody formbody = new FormBody.Builder()
                        .add("username",name)
                        .add("password",zc)
                        .add("repassword",aszc)
                        .build();

                final Request request = new Request.Builder()
                        .url("http://anli-wechat.boruiman.com/api/UserServer/user_register")
                        .post(formbody)
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("TAG", "onResponse:" +e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, final Response response) throws IOException {

                        if (!response.isSuccessful()){
                            Toast.makeText(MyRegistered.this, "请求失败", Toast.LENGTH_SHORT).show();
                        }

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                try {
                                    rePassWordBean = gson.fromJson(response.body().string(), RePassWordBean.class);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                Toast.makeText(MyRegistered.this, ""+rePassWordBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                });

            }
        }).start();
    }

    private boolean submit() {
        // validate
        name = et_name_zc.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "用戶名不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        zc = et_password_zc.getText().toString().trim();
        if (TextUtils.isEmpty(zc)) {
            Toast.makeText(this, "密碼不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        aszc = et_password_aszc.getText().toString().trim();
        if (TextUtils.isEmpty(aszc)) {
            Toast.makeText(this, "重複密碼不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

       if (!zc.equals(aszc)){
           Toast.makeText(this, "密碼不一致", Toast.LENGTH_SHORT).show();
           return false;
       }

        return true;
    }
}